/**
 * 
 */
package org.swing.utility.input.tooltip;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;
import javax.swing.ToolTipManager;

/**
 * @author Quynh Nhu
 * 
 */
public class ToolTipListener implements ComponentListener, MouseWheelListener,
		AdjustmentListener {
	/**
	 * Create a mouseMoved event to pass to the ToolTipManager.
	 */
	private void phantomMouseMoved(Component component) {
		if (component == null)
			return;

		// Mouse is in the bounds of the component, generate phantom
		// mouseMoved event for the ToolTipManager

		Point mouseLocation = component.getMousePosition();

		if (mouseLocation != null) {
			MouseEvent phantom = new MouseEvent(component,
					MouseEvent.MOUSE_MOVED, System.currentTimeMillis(), 0,
					mouseLocation.x, mouseLocation.y, 0, false);

			ToolTipManager.sharedInstance().mouseMoved(phantom);
		}
	}

	// Implement ComponentListener

	public void componentMoved(ComponentEvent e) {
		Component component = e.getComponent();
		phantomMouseMoved(component);
	}

	public void componentResized(ComponentEvent e) {
		Component component = e.getComponent();
		phantomMouseMoved(component);
	}

	public void componentHidden(ComponentEvent e) {
	}

	public void componentShown(ComponentEvent e) {
	}

	// Implement MouseWheelListener

	public void mouseWheelMoved(MouseWheelEvent e) {
		JScrollPane scrollPane = (JScrollPane) e.getSource();
		Component component = scrollPane.getViewport().getView();
		phantomMouseMoved(component);
	}

	// Implement AdjustmentListener

	public void adjustmentValueChanged(AdjustmentEvent e) {
		JScrollBar scrollBar = (JScrollBar) e.getSource();
		JScrollPane scrollPane = (JScrollPane) scrollBar.getParent();
		Component component = scrollPane.getViewport().getView();
		phantomMouseMoved(component);
	}

}